class  OperationOfDatabase{
    constructor(){
        this.res;
        this.databaseName="test_project_database";
        this.MongoClient = require('mongodb').MongoClient;
        this.url = "mongodb://localhost:27017/"+this.databaseName;
    }

    initDatabase(){
        this.MongoClient.connect(this.url, { useNewUrlParser: true ,useUnifiedTopology: true},(err, db)=> {
            if (err) throw err;
            console.log('数据库已创建');
            var dbase = db.db(this.databaseName);
            dbase.createCollection('data-users', function (err, res) {
                if (err) throw err;
                console.log("创建用户集合!");
                db.close();
                });
            dbase.createCollection('data-posts', function (err, res) {
                if (err) throw err;
                console.log("创建帖子集合!");
                db.close();
            });
            dbase.createCollection('data-comments', function (err, res) {
            if (err) throw err;
            console.log("创建评论集合!");
            db.close();
        });
});       
    }
    
    addNewUser(input_user_name,input_password,input_icon,res){
        this.MongoClient.connect(this.url, { useNewUrlParser: true , useUnifiedTopology: true}, (err, db)=> {
            if (err) throw err;
            var dbo = db.db(this.databaseName);
            var whereStr = {user_name:input_user_name};
            dbo.collection("data-users").find(whereStr).toArray((err, result)=> {
                if (err) throw err;
                db.close(); 
                if(result.length!=0){
                    res.send({status:1});
                    console.log('此用户名已经被使用');
                }else{
                    this.MongoClient.connect(this.url, { useNewUrlParser: true ,useUnifiedTopology: true}, (err, db) =>{
                        if (err) throw err;
                        
                        var dbo= db.db(this.databaseName);
                        var myobj = { user_name: input_user_name, password:input_password,icon:input_icon};
                        dbo.collection("data-users").insertOne(myobj, function(err, result) {
                            if (err) throw err;
                            db.close();
                            res.send({status:0}); 
                            console.log('注册成功');                         

                        });
                    });  
                }    
            });
        });
    }

    getUserDataByNameAndPassword(input_user_name,input_password,res){
        this.MongoClient.connect(this.url, { useNewUrlParser: true , useUnifiedTopology: true}, (err, db)=> {
            if (err) throw err;
            var dbo = db.db(this.databaseName);
            var whereStr = {user_name:input_user_name};
            dbo.collection("data-users").find(whereStr).toArray((err, result)=> {
                if (err) throw err;
                db.close();
                if(result.length==0){
                    res.send({status:2});
                    console.log('用户不存在');
                     
                }else if(result[0].password!=input_password){
                    res.send({status:1});
                    console.log('密码错误');

                }else{
                    res.send({status:0,icon:result[0].icon});
                    console.log('登录成功');                        
                }
            });
        });
    }

    addNewPost(input_user_name,input_post_title,input_add_time,input_summary,input_images,res){
        this.MongoClient.connect(this.url, { useNewUrlParser: true , useUnifiedTopology: true}, (err, db)=> {
            if (err) throw err;
            var dbo = db.db(this.databaseName);
            var whereStr = {post_title:input_post_title};
            dbo.collection("data-posts").find(whereStr).toArray((err, result)=> {
                if (err) throw err;
                db.close();
                if(result.length!=0){
                    res.send({status:1});
                    console.log("发布失败")
                }else{
                    this.MongoClient.connect(this.url, { useNewUrlParser: true ,useUnifiedTopology: true}, (err, db)=> {
                        if (err) throw err;
                        var dbo= db.db(this.databaseName);
                        var myobj = { user_name: input_user_name,post_title:input_post_title,add_time:input_add_time,summary:input_summary,images:input_images};

                        dbo.collection("data-posts").insertOne(myobj, function(err, result) {
                            if (err) throw err;
                            res.send({status:0})
                            console.log("发布成功");
                        });
                    }); 
                }
            });
        });        
        
        
     
    }

    addNewComment(input_post_title,input_add_time,input_comment_user,input_content,res){
        this.MongoClient.connect(this.url, { useNewUrlParser: true ,useUnifiedTopology: true}, (err, db)=> {
            if (err) throw err;
            var dbo= db.db(this.databaseName);
            var myobj = {post_title:input_post_title,add_time:input_add_time,comment_user:input_comment_user,content:input_content};
            dbo.collection("data-comments").insertOne(myobj, function(err, result) {
                if (err) throw err;
                db.close();
                console.log("评论成功");
                res.send({status:0});
                // this.MongoClient.connect(this.url, { useNewUrlParser: true ,useUnifiedTopology: true}, (err, db)=> {
                //     if (err) throw err;
                //     var dbo= db.db(this.databaseName);
                //     var whereStr = {post_title:input_post_title};
                //     dbo.collection("data-comments").find(whereStr).limit(10).toArray((err, result)=>  {
                //         if (err) throw err;
                //         db.close();
                //         res.send({status:0, data: result});
                        
                //     });
                // });
                
            });
        });   
    }

    getCommentsByPostTitle(input_post_title,skip_count,limit_count,res){
        this.MongoClient.connect(this.url, { useNewUrlParser: true , useUnifiedTopology: true}, (err, db)=> {
            if (err) throw err;
            var dbo = db.db(this.databaseName);
            var whereStr = {post_title:input_post_title};
            dbo.collection("data-comments").find(whereStr).sort({"add_time":-1}).skip(skip_count).limit(limit_count).toArray((err, result)=> {
                if (err) throw err;
                db.close();

                res.send({status:0, data:result});
                
            });
        });  
    }

    getAllPostsSkipAndLimit(skip_count,limit_count,res){
        this.MongoClient.connect(this.url, { useNewUrlParser: true , useUnifiedTopology: true}, (err, db)=> {
            if (err) throw err;
            var dbo = db.db(this.databaseName);
            dbo.collection("data-posts").find().sort({"add_time":-1}).skip(skip_count).limit(limit_count).toArray((err, result)=> {
                if (err) throw err;
                db.close();
                res.send({status:0, data:result});
            });
        });
    }

    getPostsByUserName(input_user_name,res){        
        this.MongoClient.connect(this.url, { useNewUrlParser: true , useUnifiedTopology: true}, (err, db)=> {
            if (err) throw err;
            var dbo = db.db(this.databaseName);
            var whereStr = {user_name:input_user_name};
            dbo.collection("data-posts").find(whereStr).sort({"add_time":-1}).toArray((err, result)=> {
                if (err) throw err;
                db.close();
                res.send({status:0, data:result});
            });
        });
    }

    getPostDetailByPostTitle(title,res){
        this.MongoClient.connect(this.url, { useNewUrlParser: true , useUnifiedTopology: true}, (err, db)=> {
            if (err) throw err;
            var dbo = db.db(this.databaseName);
            var whereStr = {post_title:title};
            dbo.collection("data-posts").find(whereStr).toArray((err, result)=> {
                if (err) throw err;
                db.close();
                res.send(result[0])
            });
        });
    }

    getPostsByPostTitle(input_post_title,res){        
        this.MongoClient.connect(this.url, { useNewUrlParser: true , useUnifiedTopology: true}, (err, db)=> {
            if (err) throw err;
            var dbo = db.db(this.databaseName);
            //var whereStr = {post_title:input_post_title};
            dbo.collection("data-posts").find().toArray((err, result)=> {
                if (err) throw err;
                db.close();
                var resArr=[];
                for(let i=0, len=result.length; i<len; i++){

                    if(result[i].post_title.indexOf(input_post_title)!=-1){
                        resArr.push(result[i]);
                    }
                }
                if(resArr.length!=0){
                    res.send({status:0, data:resArr});
                }else{
                    res.send({status:1});
                }
                
            });
        });
    }

}

module.exports = OperationOfDatabase

